﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbDoc.BusinessManager;
using DbDoc.BusinessObjects;
using System.Windows;
using System.IO;
using System.Reflection;

namespace DbDoc.PowerDoc.ViewModel
{
    public class SetupSourceViewModel : ViewModelBase, IRequestClose
    {
        public List<string> _ProviderTypes = new List<string>();
        public string ConnectionString { get; set; }
        public string ExcludedDatabases { get; set; }
        private DelegateCommand _SaveSettingsCommand;
        private Window _CurrentView;

        public List<String> ProviderType
        {
            get { return _ProviderTypes; }
            set { _ProviderTypes = value; }
        }
        public string SelectedProviderType { get; set; }

        public DelegateCommand SaveSettingsCommand
        {
            get
            {
                if (_SaveSettingsCommand == null)
                    _SaveSettingsCommand = new DelegateCommand(param => OnSaveSettingsExecuted());
                return _SaveSettingsCommand;
            }
            set { _SaveSettingsCommand = value; }
        }

        private void OnSaveSettingsExecuted()
        {
            Setting connectionString = new Setting();
            connectionString.Key = Constants.Settings.SOURCE_CONNECTIONSTRING;
            connectionString.DataType = "string";
            connectionString.RawValue = ConnectionString;
            SettingsManager.SaveSetting(connectionString);

            Setting excludedDatabases = new Setting();
            excludedDatabases.Key = Constants.Settings.EXCLUDED_DATABASES;
            excludedDatabases.DataType = "string";
            excludedDatabases.RawValue = ExcludedDatabases;
            SettingsManager.SaveSetting(excludedDatabases);

            Setting sourceType = new Setting();
            sourceType.Key = Constants.Settings.SOURCE_TYPE;
            sourceType.RawValue = SelectedProviderType;
            sourceType.DataType = "string";
            SettingsManager.SaveSetting(sourceType);

            IRequestClose closeView = (IRequestClose)_CurrentView;
            closeView.CloseMe();

        }

        public SetupSourceViewModel(Window currentWindow)
        {
            _CurrentView = currentWindow;
            DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            FileInfo[] dlls = dir.GetFiles("*.dll");
            Type sourceProviderType = Assembly.LoadFrom("Provider.Source.dll").GetTypes()[0];
            foreach (FileInfo dll in dlls)
            {

                Assembly a = Assembly.LoadFrom(dll.Name);
                Type[] types = a.GetTypes();
                foreach (Type t in types)
                    if (t.BaseType == sourceProviderType)
                        ProviderType.Add(t.Name);

            }
            //ProviderType.Add("SqlSourceProvider");
        }

        public void CloseMe()
        {
            throw new NotImplementedException();
        }

    }
}
